/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package logmatcher.testgenerators;

import bsh.EvalError;
import bsh.Interpreter;

import bsh.util.JConsole;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;


/**
 *  This class enables using BeanShell scripts.
 */
public class BeanShellTester {
    //~ Instance fields ------------------------------------------------------------------------------------------------

    /** The BeanShell JConsole. */
    JConsole console = new JConsole();

    /** The BeanShell Interpreter. */
    Interpreter interpreter = new Interpreter(console);

    /** TextArea used as an input space. */
    JTextArea inputArea = new JTextArea(10, 20);
    
    static JTextArea sippConsole = new JTextArea(30,20);

    //~ Constructors ---------------------------------------------------------------------------------------------------

/**
     *  Creates a new BeanShellTester object.
    */
    public BeanShellTester() {
        JFrame frame = new JFrame("BeanShellTester");

        inputArea.addKeyListener(
            new KeyAdapter() {
                @Override
                public void keyPressed(KeyEvent key) {
                    if (key.getKeyCode() == key.VK_ENTER) {
                        new Thread(
                            new Runnable() {
                                public void run() {
                                    String text = inputArea.getText();
                                    final String[] lines = text.split("\n");

                                    for (int i = 0; i < lines.length; i++) {
                                        final String eval = lines[i];
                                        SwingUtilities.invokeLater(
                                            new Runnable() {
                                                public void run() {
                                                    try {
                                                        interpreter.eval(eval);
                                                        console.println(eval);
                                                    } catch (EvalError e) {
                                                        e.printStackTrace();
                                                    }
                                                }
                                            });

                                        try {
                                            Thread.sleep(50);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    }

                                    inputArea.setText("");
                                    inputArea.setCaretPosition(0);
                                }
                            }).start();
                    }
                }
            });

        new Thread(interpreter).start();

		try {
	        PipedInputStream pis = new PipedInputStream();
	        PipedOutputStream pos = new PipedOutputStream(pis);
	        PrintStream ps = new PrintStream(pos);	        
	        StreamRedirector sr = new StreamRedirector(pis);
	        System.setOut(ps);
	        System.setErr(ps);	        
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        
        Font f = new Font("Courier New", Font.BOLD,12);
        sippConsole.setFont(f);
        console.setFont(f);
        inputArea.setFont(f);
        
        JSplitPane splitPane1 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,console,new JScrollPane(inputArea));
        splitPane1.setOneTouchExpandable(true);
        splitPane1.setDividerLocation(300);
        JSplitPane splitPane2 = new JSplitPane(JSplitPane.VERTICAL_SPLIT,splitPane1,new JScrollPane(sippConsole));
        splitPane2.setOneTouchExpandable(true);
        splitPane2.setDividerLocation(600);
        frame.getContentPane().add(splitPane2);
        
        frame.setDefaultCloseOperation(frame.EXIT_ON_CLOSE);
        frame.setSize(1000, 900);
        frame.setVisible(true);
    }

    //~ Methods --------------------------------------------------------------------------------------------------------

    /**
     *  The main method.
     *
     *  @param args arguments
     */
    public static void main(String[] args) {
        new BeanShellTester();
    }
    
    public static JTextArea getSippConsole(){
    	return sippConsole;
    }
}
